// pages/alumni/alumni.js
Page({
  data: {
    alumniList: [],
    keyword: '',
    contactInfo: {} ,
    currentIndustry: '全部',
    currentPosition: '',
    baseUrl: 'http://localhost:3000',
    loading: false,
    filteredAlumniList: []
  },

  onLoad: function() {
    this.loadAlumni();
  },

  onPullDownRefresh: function() {
    this.loadAlumni();
  },

  loadAlumni: function() {
    const { keyword, currentIndustry, baseUrl } = this.data;
    
    this.setData({ loading: true });

    wx.request({
      url: `${baseUrl}/api/alumni`,
      method: 'GET',
      data: {
        keyword,
        industry: currentIndustry === '全部' ? '' : currentIndustry,
        hasOpenings: true
      },
      success: (res) => {
        if (res.data.success) {
          console.log('Alumni data:', res.data.alumni);
          console.log('Has openings:', res.data.alumni.map(a => a.hasOpenings));
          const alumniWithTags = res.data.alumni.map(alumni => ({
            ...alumni,
            hasOpeningsTag: alumni.hasOpenings ? '招聘中' : ''
          }));
          
          this.setData({
            alumniList: alumniWithTags
          }, () => {
            this.updateFilteredAlumniList();
            console.log('Filtered alumni list:', this.data.filteredAlumniList);
            console.log('Filtered has openings:', this.data.filteredAlumniList.map(a => a.hasOpenings));
          });
        } else {
          wx.showToast({
            title: '获取校友列表失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('获取校友列表失败:', err);
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      },
      complete: () => {
        this.setData({ loading: false });
        wx.stopPullDownRefresh();
      }
    });
  },

  handleSearch: function(e) {
    this.setData({
      keyword: e.detail.value
    }, () => {
      this.updateFilteredAlumniList();
    });
  },

  filterByIndustry: function(e) {
    const industry = e.currentTarget.dataset.industry;
    this.setData({
      currentIndustry: industry
    }, () => {
      this.updateFilteredAlumniList();
    });
  },

  filterByPosition: function(e) {
    const position = e.currentTarget.dataset.position;
    this.setData({
      currentPosition: position === '全部' ? '' : position
    }, () => {
      this.updateFilteredAlumniList();
    });
  },

  updateFilteredAlumniList: function() {
    let { keyword, currentIndustry, currentPosition, alumniList } = this.data;
    let filteredList = alumniList;

    // 关键词搜索
    if (keyword) {
      filteredList = filteredList.filter(alumni => (
        alumni.name.includes(keyword) ||
        alumni.company.includes(keyword)
      ));
    }

    // 行业筛选
    if (currentIndustry !== '全部') {
      filteredList = filteredList.filter(alumni => 
        alumni.contact_info && 
        alumni.contact_info.industry === currentIndustry
      );
    }

    // 职位筛选
    if (currentPosition) {
      filteredList = filteredList.filter(alumni => {
        if (!alumni.contact_info) return false;
        
        const position = alumni.contact_info.position || '';
        if (currentPosition === '产品经理') {
          return position.includes('产品经理') || position.includes('Product Manager');
        } else if (currentPosition === '工程师') {
          return position.includes('工程师') || position.includes('Engineer');
        } else if (currentPosition === '高级工程师') {
          return position.includes('高级工程师') || position.includes('Senior Engineer');
        }
        return position === currentPosition;
      });
    }

    this.setData({ filteredAlumniList: filteredList });
  },

  navigateToDetail: function(e) {
    const alumniId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/alumni-detail/alumni-detail?id=${alumniId}`
    });
  }
});
